package models

import (
	"fmt"
	"math/rand"
	"time"
)

// 订单表
type Order struct {
	Id               int            `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT;comment:订单ID" json:"id"`
	AdminId          int            `gorm:"column:admin_id;type:int(11);default:0;comment:录入人;NOT NULL" json:"admin_id"`
	OrderNo          string         `gorm:"column:order_no;type:varchar(255);comment:订单号;NOT NULL" json:"order_no"`
	Source           string         `gorm:"column:source;type:varchar(255);comment:订单来源（美团、京东等）;NOT NULL" json:"source"`
	CustomerName     string         `gorm:"column:customer_name;type:varchar(100);comment:客户名称;NOT NULL" json:"customer_name"`
	SignerName       string         `gorm:"column:signer_name;type:varchar(50);comment:签单人;NOT NULL" json:"signer_name"`
	SignDate         *time.Time     `gorm:"column:sign_date;type:date;comment:签单日期" json:"sign_date"`
	PushNo           string         `gorm:"column:push_no;type:varchar(255);comment:推送编号;NOT NULL" json:"push_no"`
	PushDate         *time.Time     `gorm:"column:push_date;type:date;comment:推送日期" json:"push_date"`
	PushTime         string         `gorm:"column:push_time;type:varchar(20);comment:推送时间;NOT NULL" json:"push_time"`
	CompleteDate     *time.Time     `gorm:"column:complete_date;type:date;comment:完成时间" json:"complete_date"`
	TaxpayerNo       string         `gorm:"column:taxpayer_no;type:varchar(30);comment:纳税人识别号;NOT NULL" json:"taxpayer_no"`
	ProjectRecipient string         `gorm:"column:project_recipient;type:varchar(50);comment:项目接收人;NOT NULL" json:"project_recipient"`
	LayoutPerson     string         `gorm:"column:layout_person;type:varchar(50);comment:排版人;NOT NULL" json:"layout_person"`
	Registrant       string         `gorm:"column:registrant;type:varchar(50);comment:备案人;NOT NULL" json:"registrant"`
	RegistrationNo   string         `gorm:"column:registration_no;type:varchar(255);comment:备案号;NOT NULL" json:"registration_no"`
	LegalPersonName  string         `gorm:"column:legal_person_name;type:varchar(50);comment:法人姓名;NOT NULL" json:"legal_person_name"`
	LegalPersonPhone string         `gorm:"column:legal_person_phone;type:varchar(20);comment:法人电话;NOT NULL" json:"legal_person_phone"`
	LegalPersonIdno  string         `gorm:"column:legal_person_idno;type:varchar(30);comment:法人身份证号;NOT NULL" json:"legal_person_idno"`
	SettlementMethod string         `gorm:"column:settlement_method;type:varchar(50);comment:结算方式 先付款 先服务;NOT NULL" json:"settlement_method"`
	ConsigneeCity    string         `gorm:"column:consignee_city;type:varchar(50);comment:所在城市;NOT NULL" json:"consignee_city"`
	SignCompany      string         `gorm:"column:sign_company;type:varchar(100);comment:签约公司;NOT NULL" json:"sign_company"`
	TotalAmount      float64        `gorm:"column:total_amount;type:decimal(12,2);default:0.00;comment:订单总金额;NOT NULL" json:"total_amount"`
	Status           int            `gorm:"column:status;type:tinyint(4);default:1;comment:状态：1制作中2物流中3已完成4已取消;NOT NULL" json:"status"`
	Attachments      string         `gorm:"column:attachments;type:json;comment:附件（如图片其他信息）" json:"attachments"`
	OriginalContent  string         `gorm:"column:original_content;type:text;comment:原始内容" json:"original_content"`
	RecipientName    string         `gorm:"column:recipient_name;type:varchar(100);comment:收件人姓名;NOT NULL" json:"recipient_name"`
	RecipientPhone   string         `gorm:"column:recipient_phone;type:varchar(30);comment:收件人电话;NOT NULL" json:"recipient_phone"`
	RecipientCity    string         `gorm:"column:recipient_city;type:varchar(255);comment:所在城市;NOT NULL" json:"recipient_city"`
	RecipientAddress string         `gorm:"column:recipient_address;type:varchar(255);comment:收件人地址;NOT NULL" json:"recipient_address"`
	CreatedTime      time.Time      `gorm:"column:created_time;type:timestamp;default:CURRENT_TIMESTAMP;NOT NULL" json:"created_time"`
	UpdatedTime      time.Time      `gorm:"column:updated_time;type:timestamp;default:CURRENT_TIMESTAMP;NOT NULL" json:"updated_time"`
	Courier          []OrderCourier `gorm:"foreignKey:order_no;references:order_no" json:"-"`
	Product          []OrderProduct `gorm:"foreignKey:order_no;references:order_no" json:"-"`
}

func (m *Order) TableName() string {
	return "order"
}

// 订单状态常量
const (
	OrderStatusProducing = 1 // 制作中
	OrderStatusShipping  = 2 // 物流中
	OrderStatusFinished  = 3 // 已完成
	OrderStatusCanceled  = 4 // 已取消
)

// 订单状态对应中文描述
var OrderStatusMap = map[int]string{
	OrderStatusProducing: "制作中",
	OrderStatusShipping:  "物流中",
	OrderStatusFinished:  "已完成",
	OrderStatusCanceled:  "已取消",
}

type Attachments struct {
	Name string `json:"name"`
	Url  string `json:"url"`
}

// GenerateOrderNo 生成订单号
// GenerateOrderNo 生成短日期 + 随机字符串的订单号
func GenerateOrderNo() string {
	currentTime := time.Now()
	datePart := currentTime.Format("060102150405") // 'yyMMddHHmmss'
	// 将微秒转换为字符串并取前 4 位
	microPart := fmt.Sprintf("%05d", rand.Intn(9000)+10000)
	microPart2 := fmt.Sprintf("%05d", rand.Intn(9000)+10000)
	return fmt.Sprintf("OR%s%s%s", datePart, microPart, microPart2)

}
